package com.heitian.ssm.controller;

import com.heitian.ssm.model.BathEntity;
import com.heitian.ssm.model.BathManagerEntity;
import com.heitian.ssm.model.UserEntity;
import com.heitian.ssm.service.BathManagerService;
import com.heitian.ssm.service.BathService;
import com.heitian.ssm.service.UserLoginService;
import com.heitian.ssm.utils.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: mishuai
 * Date: 2018-04-24
 * Time: 16:45
 * Des:
 */
@Controller
@RequestMapping("/admin")
public class BathController {
    private Logger log = Logger.getLogger(BathController.class);
    @Resource
    private BathService bathService;

    @Resource
    private UserLoginService userLoginService;

    @Resource
    private BathManagerService bathManagerService;

    @RequestMapping("/bathOrder")
    @ResponseBody
    public void selectByTime(HttpServletRequest request, HttpServletResponse response){
        log.info("入参");
        List<BathEntity> bathEntities=null;
        try{
           bathEntities= bathService.selectByCreatTime();
        }catch (Exception e){
            log.error(e);
            e.printStackTrace();
        }
        JsonResultUtils.ajaxReturnJsonP(request,response,bathEntities);
    }


    @RequestMapping("/byBathId")
    @ResponseBody
    public void selectByBathId(HttpServletRequest request, HttpServletResponse response){

        String bathId=request.getParameter("bathId");

        log.info("入参"+bathId);
        BathEntity bathEntities=null;
        try{
            bathEntities= bathService.selectByBathId(bathId);
        }catch (Exception e){
            log.error(e);
            e.printStackTrace();
        }
        JsonResultUtils.ajaxReturnJsonP(request,response,bathEntities);
    }

    @RequestMapping("/bathRoom")
    @ResponseBody
    public void selectByBathRoom(HttpServletRequest request, HttpServletResponse response){
        log.info("入参");
        List<BathManagerEntity> bathEntities=null;
        try{
            bathEntities= bathManagerService.bathManagerAll();
        }catch (Exception e){
            log.error(e);
            e.printStackTrace();
        }
        JsonResultUtils.ajaxReturnJsonP(request,response,bathEntities);
    }

    @RequestMapping("/backInput")
    @ResponseBody
    public void backInput(HttpServletRequest request, HttpServletResponse response){

        String bathId=request.getParameter("bathId");

        log.info("入参"+bathId);
        BathManagerEntity bathEntities=null;
        try{
            bathEntities= bathManagerService.selectByBathId(bathId);
        }catch (Exception e){
            log.error(e);
            e.printStackTrace();
        }
        JsonResultUtils.ajaxReturnJsonP(request,response,bathEntities);
    }
    @RequestMapping("/updateInput")
    @ResponseBody
    public void updateInput(HttpServletRequest request, HttpServletResponse response){

        String bathId=request.getParameter("bathID");
        String bathStatuS=request.getParameter("bathStatuS");

        int i=-1;
        try {
            i=bathManagerService.updateByPrimaryKey(bathId,bathStatuS);
            JedisUtil.setString(bathId,"03");
        }catch (Exception e){
            log.error(e);
        }

        if(i<=0){
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_99999","更新数据失败")));
        }else {

            JsonResultUtils.ajaxReturn(response,FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_00000","更新数据成功")));
        }
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public void selectAll(HttpServletRequest request, HttpServletResponse response){


        log.info("查询澡位情况入参");

        List<String> list=new ArrayList<String>();
        String []str ={"101","102","103","104","105","106","107","108","109","110","111","112",
                       "113","114","115","116","117","118","119","120","121","122","123","124",
                       "125","126","127","128"};
        for(int i=0;i<str.length;i++){
            if(JedisUtil.getString(str[i]) !=null){
                list.add(str[i]+"|"+JedisUtil.getString(str[i]));
            }
        }
        log.info("查询澡位情况出参"+FastJsonUtils.toJSONString(list));
        JsonResultUtils.ajaxReturnJsonP(request,response,list);
    }


    @RequestMapping("/selectBath")
    @ResponseBody
    public void selectBath(HttpServletRequest request, HttpServletResponse response){
        SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String bathId=request.getParameter("bathId");
        String userName=request.getParameter("userName");
        String startTime=request.getParameter("selectStartTime");
        String endTime=request.getParameter("selectEndTime");
        log.info("入参信息"+"bathId:"+bathId+" "+"userName"+userName+" "+"startime"+startTime+" "+"endtime"+endTime);
        Date stratDate=null;
        Date endDate=null;
        long interval=0;

        UserEntity userCard=userLoginService.getUserCard(userName);
        try {
            stratDate=myFmt2.parse(startTime);
            endDate=myFmt2.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(stratDate != null && endDate != null && (stratDate.getTime() - endDate.getTime()) < 0){
            interval = (endDate.getTime() - stratDate.getTime())/1000;

        }else {
            log.info("时间选择错误");
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_00000","bathTimeOut")));
        }
        int intervalInt=(int)interval;
        if(JedisUtil.getString(bathId) != null){
            if(JedisUtil.getString(bathId).equals("02")){
                log.info("预订失败，已被预订");
                JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_00000","bath02")));
            }
            if(JedisUtil.getString(bathId).equals("03")){
                log.info("预订失败，设备损坏");
                JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_00000","bath03")));
            }
        }else{
            log.info("入参"+bathId);

            BathEntity bathEntities=new BathEntity();
            bathEntities.setSelectStartTime(stratDate);
            bathEntities.setSelectEndTime(endDate);
            bathEntities.setBathUserCardno(userCard.getUserCardNo());
            bathEntities.setBathId(bathId);

            try{
                int i= bathService.insertSelective(bathEntities);
                if(i > 0){
                    log.info("时间秒数："+intervalInt);
                    JedisUtil.setString(bathId,intervalInt,"02");
                    log.info("选择成功"+"++++++++++++"+JedisUtil.getString(bathId)+"暴力"+JedisUtil.getString("120"));
                    try {
                        MailUtil.send_mail(userCard.getUserEmail(),"尊敬的"+userCard.getUserName()+"用户：感谢您注册我们的网站<br>"+"请于"+bathEntities.getSelectStartTime()+"前到达");
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    }
                    JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_00000","bath01")));
                }
                 }catch (Exception e){
                log.error(e);
                e.printStackTrace();
            }


        }



    }

}
